package com.bm.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bm.book.dao.IBookDao;
import com.bm.book.dao.IBookDocDao;
import com.bm.book.service.IBookService;
import com.bm.domain.doc.BookDoc;
import com.bm.domain.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements IBookService {
    @Autowired
    IBookDao iBookDao;
    @Autowired
    IBookDocDao iBookDocDao;

    @Override
    public List<Book> findLikeBy(String name) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        if(name!=null && !name.equals("")){
            wrapper.like(Book::getTitle,name).or().like(Book::getIntroduction,name)
                    .or().like(Book::getAuthor,name);
        }
        List<Book> books =iBookDao.selectList(wrapper);
        return books;
    }

    @Override
    public void addToEs() {
        List<Book> books =iBookDao.selectList(null);
        books.forEach(b->{
            BookDoc bookDoc = new BookDoc();
            bookDoc.setId(String.valueOf(b.getId()));
            bookDoc.setAuthor(b.getAuthor());
            bookDoc.setIntroduction(b.getIntroduction());
            bookDoc.setTitle(b.getTitle());
            bookDoc.setPrice(b.getPrice());
            iBookDocDao.save(bookDoc);
        });
    }

    @Override
    public List<BookDoc> findFromEs(String name) {
        iBookDocDao.findAll();
        return null;
    }
//    @Autowired
//    IBookTypeService iBookTypeService;
//
//    @Autowired
//    IPublisherService iPublisherService;

//    private static Map<Integer, BookType> bookTypeMap= new HashMap<>();//图书类型
//    private static Map<Integer, Publisher>publisherMap = new HashMap<>();
//    private void findAllTypes(){
//        List<BookType> bookTypes = iBookTypeService.findAll();
//        bookTypes.forEach((i)->{
//            bookTypeMap.put(i.getId(),i);
//        });
//        List<Publisher> publishers=iPublisherService.findAll();
//        publishers.forEach(i->{
//            publisherMap.put(i.getId(),i);
//        });
//    }
//    @Override
//    public List<BookVo> findAll(String searchkey) {
//        findAllTypes();
//        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
//        if(searchkey!=null && !searchkey.equals("")){
//            wrapper.like(Book::getTitle,searchkey).or().like(Book::getIntroduction,searchkey);
//        }
//        List<Book> books =iBookDao.selectPage(page,wrapper);
//        List<BookVo> list = new ArrayList<>(50);
//        books.forEach(b->{
//            list.add(new BookVo(b, bookTypeMap.get(b.getType()), publisherMap.get(b.getPublisher()) ));
//        });
//        return list;
//    }

}
